import DWC_DCCMsgList from "./DWC_DCCMsgList";
import { DWC_DCContentData } from "./DWC_DCContentItem/DWC_DCContentItem";

const { ccclass, property } = cc._decorator;
/** 复杂聊天 聊天界面 (没有聊天界面) create by tm */
@ccclass
export default class DWC_DCContent extends cc.Component {
    //-----------组件区----------
    @property(DWC_DCCMsgList)
    msgList: DWC_DCCMsgList = null;
    //-----------变量区----------
    contentParent: cc.Node = null;
    /** 聊天类型 1 私聊消息(玩家) 2 桌子消息(房间) 3 大厅消息 */
    chatType: number = null;
    /** 聊天id 
     * chatType == 1时,为接收者 rid
     * chatType == 2时,为接收者 桌子ID
     * chatType == 3时,为接收者 所属代理ID
    */
    chatId: any = null;
    /** 玩家自己信息 */
    _player: any = null;
    onLoad() {
        this.addEvent();
        this.initData();
    }
    /** 添加事件 */
    addEvent(): void {
    }
    /** 移除事件 */
    removeEvent(): void {

    }
    /** 初始化数据 */
    initData(): void {
        this._player = cc.vv.session.getPlayer();
    }
    /** 初始化(外界调用) */
    init(_args): void {
        this.contentParent = _args.contentParent;
        this.setChatTypeId(_args.chatType, _args.chatId);
    }
    /** 设置聊天类型和聊天id */
    setChatTypeId(_type: number, _chatId: any): void {
        this.chatType = _type;
        this.chatId = _chatId;
    }
    //----------外部事件区--------
    /** 玩家发送消息 */
    playerSendMsg(_sendMsg: any): void {
        if (!_sendMsg || !this.chatType) return;
        let _content: string = _sendMsg.content;
        //聊天类型 (1:文本;2:表情;3:语音;)
        switch (_sendMsg.chatType) {
            case 2:
                _content = cc.vv.session.txtEmoticon(_content, true);
                break;
        }
        let _msg = {
            type: this.chatType,// 消息类型 1 私聊消息 2 桌子消息 3 大厅消息
            content: _content,
        }
        switch (this.chatType) {
            case 1://玩家私聊
                _msg[`receiver_rid`] = this.chatId;
                break;
            case 2://房间内聊天
                _msg[`table_id`] = `${this.chatId}`;
                break;
            case 3://大厅聊天
                _msg[`agent_id`] = `${this.chatId}`;
                break;
        }
        cc.vv.GLUtils.sendRequest(`ATTChat`, _msg, (_err, _data) => {
            if (!_data.errordes) {//成功
                //自己的消息
                let _mySelf = {
                    style: 2,
                    type: _sendMsg.chatType,
                    nick: this._player.rolename,
                    content: _sendMsg.content,
                    head: this._player.logo,
                    rid: this._player.rid,
                    itemY: 1,
                    height: -1,
                }
                this.addChatContent([_mySelf], true, true);
            } else {
                //保存没有发送成功的消息
                cc.vv.GLUtils.showTips(_data.errordes);
            }
        });
    }
    /** 添加聊天内容*/
    addChatContent(_contents: DWC_DCContentData[], _isAdd: boolean, _isPush: boolean): void {
        if (!_contents) return;
        _isAdd ? this.msgList.addItem(_contents, _isPush) : this.msgList.initData(_contents);
    }
    /** 滚动回调事件 */
    scrollEvent(_scroll: cc.ScrollView, _evttype: cc.ScrollView.EventType, _data): void {
        switch (_evttype) {
            case 9://滚动结束
                this.msgList.scrollMove(false);
                break;
            case 12://滚动开始
                this.msgList.scrollMove(true);
                break;
        }
    }
    /** 储存聊天消息 */
    storageChatData(_chatId: any, _chatType: number, _chat: DWC_DCContentData): void {
        cc.vv.session.setChatData(_chatId, _chatType, _chat);
    }
    onDestroy() {
        this.removeEvent();
    }
}
